<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: LJQ <245369209@qq.com> 
// +----------------------------------------------------------------------

namespace app\plan\controller\api;
use app\common\controller\Api;
use think\response\Json;

/**
 * 执行记录管理
 * @package app\record
 * @author LJQ<245369209@qq.com>
 */
class Record extends Api

{

    /**
   *  执行记录列表
   * @param string 
   * @return Json
   */

	public function recordList($recordId=null,$enterpriseId=null){
        $where= [] ;
        $with='events,records';
        $where['typeId']=1;

        if ($recordId) {
            $where['id']=['eq',$recordId];
        }
        //分页参数
        if(($limit = input('limit')) && ($page = input('page'))){
            $startLimit =  ($page-1)*$limit;
            // 救援行动列表
            $info =collection(model('plan/record')->with('events')->with('records.unit')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
        }else{
            //救援行动列表
            $info =collection(model('plan/record')->with('events')->with('records.unit')->where($where)->select())->toArray();
        }
	   	 
	 	//总数
	   	 $count = model('plan/record')->where($where)->count('id');
		
    		      if ($info && $count == 0) {
         			return json(['code'=>1,'msg'=>'没有找到执行记录','count'=>$count,'data'=>$info]);
       		} 
       	
       		if ($count > 0){
       			return json(['code'=>1,'msg'=>'执行记录','count'=>$count,'data'=>$info]);
       		}
            		return json(['code'=>0,'msg'=>'数据异常']);
   	 	 }
   	  
   	      //添加执行记录
   	 public function add($data){

          if (empty($data)) {
              return json(['code'=>0,'msg'=>'数据异常,请检查']);
          }
          $model = model('Plan/record');

          $result = $model->change($data);
          if (false != $result) {
              setlog('recordadd','record',session('user_auth.id'));
              return json(['code'=>1,'msg'=>'执行记录添加成功','recordId'=>$model->id]);
          } else {
              return json(['code'=>0,'msg'=>'执行记录添加失败,请检查']);
          }
      }

   	

   /**
   * 修改执行记录
   * @param  string recordId 执行记录id,
   * @return Json
   */
   	public function edit($data,$recordId){
        
          if (empty($recordId)) {
              return json(['code'=>0,'msg'=>'执行记录不存在,请检查']);
          }
          $model = model('record/record');     
          if ($model->change($data)) {
            setlog('recordupdate','record/record',session('user_auth.id'));
             return json(['code'=>1,'msg'=>'执行记录修改成功']);
            }else{
             return json(['code'=>0,'msg'=>'执行记录执行记录修改失败,请检查']);
            }
    }
    /**
   *  删除执行记录
   * @param string recordId 执行记录id,
   * @param Json
   */
     
    public function del($recordId) {
        $recordId = input('recordId',0,'intval');
        $model = model('plan/record');
      	if (empty ( $recordId )||empty(($model->where(array('id'=>$recordId))->find()))) {
           	return  json(['code'=>0,'msg'=>'执行记录id不存在,请检查']);
        }
        if($model->destroy(['id'=>$recordId]) !== FALSE){
          $res=model('plan/recorddetail')->where(array('rescueExecId'=>$recordId))->delete();
          setlog('recorddel','record/record',session('user_auth.id'));
        	 return json(['code'=>1,'msg'=>'执行记录删除成功']);
       }else{
         	 return $this->json(['code'=>0,'msg'=>'执行记录删除失败,请检查']);
       }
    }

     
   /**
   *   查询执行记录
   * @param keyword执行记录名称
   * @return Json
   */
   	public function search($typeId=null,$enterpriseId=null){
   		$where = [] ;

		if($typeId){
			
			$where['typeId'] = ['eq',$typeId];
		}

                if($enterpriseId){
      
                  $where['unitId'] = ['eq',$enterpriseId];
            }




   		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			//执行记录列表
			$info =collection(model('record')->with('enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			//执行记录列表
			$info =collection(model('record')->with('enterprise')->where($where)->select())->toArray();
		}
	 	//总数
	       $count = model('record')->where($where)->count('id'); 
		 
    	       if (empty($info) && $count == 0) {
              	return json(['code'=>0,'msg'=>'没有找到执行记录','count'=>$count]);
       	} 
       	
       	if ($count > 0){
       		return json(['code'=>1,'msg'=>'执行记录','count'=>$count,'data'=>$info]);
       	}
      }
}